//
//  GroupInfoTopCell.m
//  SportSocial
//
//  Created by yull on 15/1/12.
//  Copyright (c) 2015年 yull. All rights reserved.
//

#import "GroupInfoTopCell.h"
#import "NSString+Size.h"
#import <UIImageView+WebCache.h>
#import "StarView.h"

@implementation GroupInfoTopCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        CGRect rect = CGRectMake(10, 10, 80, 80);
        self.iconImageView = [[UIImageView alloc] initWithFrame:rect];
        [self.contentView addSubview:self.iconImageView];
        
        rect = CGRectMake(0, self.iconImageView.height - 15, self.iconImageView.width, 15.0f);
        self.iconCountLabel = [[UILabel alloc] initWithFrame:rect];
        self.iconCountLabel.backgroundColor = [UIColor clearColor];
        self.iconCountLabel.textAlignment = NSTextAlignmentRight;
        self.iconCountLabel.font = [UIFont systemFontOfSize:14.0f];
        self.iconCountLabel.textColor = [UIColor whiteColor];
        [self.iconImageView addSubview:self.iconCountLabel];
        
        CGFloat orginx = self.iconImageView.right + 10;
        rect = CGRectMake(orginx, 15.0f, self.contentView.width - orginx - self.iconImageView.left, 36);
        self.nameLabel = [[UILabel alloc] initWithFrame:rect];
        self.nameLabel.backgroundColor = [UIColor clearColor];
        self.nameLabel.textColor = [UIColor blackColor];
        self.nameLabel.font = [UIFont boldSystemFontOfSize:18.0f];
        self.nameLabel.numberOfLines = 0;
        [self.contentView addSubview:self.nameLabel];
        
        rect = CGRectMake(self.nameLabel.left, self.nameLabel.bottom + 10, self.contentView.width - self.nameLabel.left - 10, 20);
        self.countLabel = [[UILabel alloc] initWithFrame:rect];
        self.countLabel.textColor = [UIColor grayColor];
        self.countLabel.font = [UIFont systemFontOfSize:16.0f];
        self.countLabel.backgroundColor = [UIColor clearColor];
        self.countLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        [self.contentView addSubview:self.countLabel];
        
        self.separatorInset = UIEdgeInsetsMake(0, self.nameLabel.left, 0, 0);
    }
    return self;
}

+ (CGFloat)cellHeight
{
    return 90.0f;
}

+ (CGFloat)calulateHeightWithText:(NSString *)text
{
    CGSize constrainedSize = CGSizeMake(SCREEN_WIDTH - 100 - 10, CGFLOAT_MAX);
    
    CGSize textSize = [text getSizeWithFont:[UIFont boldSystemFontOfSize:18.0f] constrainedSize:constrainedSize];
    CGFloat height = textSize.height + 55;
    
    return MAX(height, 90.0f);
}

- (void)drawCellWithGroup:(NearGroup *)group
{
    if (group.remark.length > 0) {
        self.nameLabel.text = group.remark;
    }else {
        self.nameLabel.text = group.name;
    }
    
    self.countLabel.text = [[NSString alloc] initWithFormat:@"成员 %d人",group.user_count];
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:group.icon] placeholderImage:[UIImage imageNamed:@"julebu"]];
    
    CGSize constrainedSize = CGSizeMake(SCREEN_WIDTH - 100 - 10, CGFLOAT_MAX);
    
    CGSize textSize = [self.nameLabel.text getSizeWithFont:[UIFont boldSystemFontOfSize:18.0f] constrainedSize:constrainedSize];
    self.nameLabel.height = textSize.height;
    
    self.countLabel.top = self.nameLabel.bottom + 10;
}

- (UIEdgeInsets)layoutMargins
{
    return UIEdgeInsetsMake(0, self.nameLabel.left, 0, 0);
}

@end
